<template>
  <div class="form">
    <el-form :model="form" :inline="true">
      <el-form-item label="公司商品ID:">
        <el-input v-model="companyGoodIds" clearable style="width: 175px;" />
      </el-form-item>
      <el-form-item label="店铺商品ID:">
        <el-input v-model="shopGoodIds" clearable style="width: 175px;" />
      </el-form-item>
      <el-form-item label="店铺SKUID:">
        <el-input v-model="shopGoodSkuIds" clearable style="width: 205px;" />
      </el-form-item>
      <el-form-item label="商品名称:">
        <el-input v-model="form.name" clearable style="width: 205px;" />
      </el-form-item>
      <el-form-item label="商品类型:">
        <el-select v-model="form.typeId" clearable>
          <el-option label="全部" :value="0"></el-option>
          <el-option :label="item.value" :value="item.key" v-for="item in productTypeDics" :key="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库:">
        <el-select v-model="form.warehouseId" clearable>
          <el-option label="全部" :value="0"></el-option>
          <el-option :label="item.value" :value="item.key" v-for="item in warehouseDics" :key="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="归属公司:">
        <el-select v-model="form.company" clearable style="width: 255px;">
          <el-option :label="item.value" :value="item.key" v-for="item in companyDics" :key="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-button type="primary" @click="shopMerge">产品合并</el-button>
    <el-button type="primary" @click="bulkTakeDelivery">批量档口拿货</el-button>
    <el-button type="primary" @click="bulkSet">批量设置档口</el-button>
  </div>
  <div class="tableDiv">
    <el-table ref="multipleTableRef" :data="tableData" border @selection-change="handleSelectionChange" max-height="650"
      :row-class-name="tableRowClassName" style="width: 1760px;" v-loading="loading">
      <el-table-column type="selection" width="55" />
      <el-table-column label="SKU图片" width="120">
        <template #default="scope">
          <el-popover placement="top-start" :width="340" trigger="hover">
            <template #reference>
              <el-image style="width: 40px; height: 40px" :src="scope.row.skuPicture" fit="fill" />
            </template>
            <template #default>
              <el-image style="width: 300px; height: 300px" :src="scope.row.skuPicture" fit="fill" />
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="产品类型" width="120">
        <template #default="scope">
          <el-select v-model="scope.row.typeId" clearable @change="setWarnNum(scope.row)">
            <el-option :label="item.value" :value="item.key" v-for="item in productTypeDics" :key="item.key" />
          </el-select>
          <!-- <span v-for="item in productTypeDics" :key="item.key">{{
      scope.row.typeId == item.key ? item.value : '' }}</span> -->
        </template>
      </el-table-column>
      <el-table-column label="产品分类" width="120">
        <template #default="scope">{{ scope.row.classify }}</template>
      </el-table-column>
      <el-table-column label="所在仓库" width="120">
        <template #default="scope"> <span v-for="item in warehouseDics" :key="item.key">{{
          scope.row.warehouseId == item.key ? item.value : '' }}</span> </template>
      </el-table-column>
      <el-table-column label="归属公司" width="120">
        <template #default="scope">{{ scope.row.company }}</template>
      </el-table-column>
      <el-table-column label="公司商品ID" width="180">
        <template #default="scope">{{ scope.row.companyGoodId }}</template>
      </el-table-column>
      <el-table-column label="店铺商品ID" width="180">
        <template #default="scope">{{ scope.row.shopGoodId }}</template>
      </el-table-column>
      <el-table-column label="店铺SKUID" width="180">
        <template #default="scope">{{ scope.row.shopGoodSkuId }}</template>
      </el-table-column>
      <el-table-column label="SKU信息" width="120">
        <template #default="scope">{{ scope.row.skuInfo }}</template>
      </el-table-column>
      <el-table-column label="库存数" width="120">
        <template #default="scope">{{ scope.row.stockNum }}</template>
      </el-table-column>
      <el-table-column label="库存预警数" width="120">
        <template #default="scope"> <input v-model="scope.row.stockWarnNum" class="headclass"
            style="width: 90px;border:none" @blur='setWarnNum(scope.row)' />
        </template>
      </el-table-column>
      <el-table-column width="120">
        <template #header>
          <span>最近销量</span>
          <el-icon @click="SortBtn">
            <Sort />
          </el-icon>
        </template>
        <template #default="scope">{{ scope.row.lastSalesNum }}</template>
      </el-table-column>
      <el-table-column label="录入时间" width="120">
        <template #default="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column label="操作" width="240">
        <template #default="scope">
          <el-button link type="primary" size="small" @click.prevent="lookDetails(scope.row)">
            详情
          </el-button>
          <el-button link type="primary" size="small" v-if="scope.row.typeId === '2' && !scope.row.isWarehouseInfo"
            @click.prevent="BindingRepositoryLocation(scope.row)">
            绑定库位
          </el-button>
          <span v-if="scope.row.typeId == '5'">
            <el-button link type="primary" size="small" @click.prevent="openStallDiaog(scope.row)">
              档口拿货
            </el-button>
            <el-button link type="primary" size="small" @click.prevent="stallReturn(scope.row)">
              档口退货
            </el-button>
          </span>
          <span v-else-if="scope.row.typeId === '3'">
            <el-button link type="primary" size="small" @click.prevent="GeneratePurchaseOrder(scope.row)">
              生成采购单
            </el-button>
            <el-button link type="primary" size="small" @click.prevent="createReturnOrder(scope.row)">
              生成退货信息
            </el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="page">
    <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
  <!-- 生成采购单 -->
  <div>
    <el-dialog v-model="OrderDialogVisible" width="1160">
      <div>
        <el-form :model="OrderForm" label-width="auto" :inline="true">
          <el-form-item label="需采购总数量:">
            <el-input v-model="OrderForm.purchaseNum" />
          </el-form-item>
          <el-form-item label="需分成:">
            <el-input v-model="OrderForm.orderNum" />
          </el-form-item>
          <el-form-item label="发送到:">
            <el-select v-model="OrderForm.warehouseId" clearable>
              <el-option :label="item.value" :value="item.key" v-for="item in warehouseDics" :key="item.key" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="OrderDialogOnSubmit">确定</el-button>
          </el-form-item>
        </el-form>
        <div class="NUM" v-if="increaseNumber">
          <div ref="NUMDom" v-for="(item, index) in numsKey   " :key='index'>
            此单采购件数:<el-input v-model="numsKeyObj[index]" type="number" style="width: 200px;" /><el-icon
              @click="upNum(NUMDom)" v-if="numsKey != OrderForm.orderNum">
              <Plus />
            </el-icon>
          </div>
          <div style="margin: 160px 0px 0px 60px;">
            <el-button type="primary" @click="saveOrderNum">保存</el-button>
            <el-button type="primary" @click="cancelBtn">取消</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
  <!-- 档口拿货 -->
  <div>
    <el-dialog v-model="stallDialogVisible" width="700">
      <span>
        <div style="display: flex;align-items: center;">
          <span>需拿货总数量：</span><el-input style="width: 200px;" v-model="purchaseNum" clearable />
          <span>选择档口：</span>
          <el-select v-model="productGearId" clearable>
            <el-option :label="item.value" :value="item.key" v-for="item in store.productGearListTable"
              :key="item.key" />
          </el-select>
          <el-button type="primary" @click="productGearPurchaseCreateSubmit">
            确定
          </el-button>
        </div>
      </span>
    </el-dialog>
  </div>
  <!-- 退货信息 -->
  <el-dialog v-model="CreateReturndialogVisible" width="1200">
    <span>
      <el-table ref="multipleTableRef" :data="CreateReturntableData" style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column label="入库批次号" width="120">
          <template #default="scope">{{ scope.row.inboundBatchesCode }}</template>
        </el-table-column>
        <el-table-column label="采购单号" width="">
          <template #default="scope">{{ scope.row.productPurchaseOrderCode }}</template>
        </el-table-column>
        <el-table-column label="采购时间" width="180">
          <template #default="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column label="采购数量" width="120">
          <template #default="scope">{{ scope.row.purchaseNum }}</template>
        </el-table-column>
        <el-table-column label="采购账号" width="">
          <template #default="scope">{{ scope.row.purchaseCustomerName }}</template>
        </el-table-column>
        <el-table-column label="采购单价" width="120">
          <template #default="scope">{{ scope.row.purchasePrice / 100 + '元' }}</template>
        </el-table-column>
        <el-table-column label="退货数量" width="120">
          <template #default="scope"> <input v-model="scope.row.processQualityBouncedNum" class="headclass"
              style="width: 90px;border:none" /></template>
        </el-table-column>
      </el-table>
    </span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="CreateReturndialogVisible = false">取消</el-button>
        <el-button type="primary" @click="CreateReturndialogVisibleSubmit">
          确定退货
        </el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 绑定库位 -->
  <div>
    <el-dialog v-model="dialogVisibles" width="1200">
      <span>
        <div ref="NUMDom" v-for="(item, index) in setWareHouseform   " :key='index'
          style="display: flex; align-items: center;">
          仓库：<el-select v-model="setWareHouseform[index].warehouseId">
            <el-option :label="item.value" :value="item.key" v-for="item in WareHouseArr.warehouseDicList"
              @click="setKuqu(item.warehouseAreaDicList)" :key="item.key" />
          </el-select>
          库区：<el-select v-model="setWareHouseform[index].warehouseAreaId">
            <el-option :label="item.value" :value="item.key" v-for="item in kuqu" :key="item.key"
              @click="setKuwei(item.warehouseLocationDicList)" />
          </el-select>
          库位：<el-select v-model="setWareHouseform[index].id">
            <el-option :label="item.value" :value="item.key" v-for="item in kuwei" :key="item.key" />
          </el-select>
          库存:<el-input type="number" style="width: 200px;" v-model="setWareHouseform[index].cargoMargin" />
          <el-icon @click="upNumw(NUMDom)" size="30">
            <CirclePlusFilled />
          </el-icon>
          <el-icon @click="downNum(index)" style="margin-left: 20px;" size="30">
            <RemoveFilled />
          </el-icon>
        </div>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisibles = false">取消</el-button>
          <el-button type="primary" @click="submintDialog">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
  <div>
    <el-dialog v-model="shopeTypeValueDialogVisible" width="500">
      <span>
        产品类型不同需要合并：<el-select v-model="shopeTypeValue" size="large" style="width: 240px">
          <el-option :label="item.value" :value="item.key" v-for="item in productTypeDics" :key="item.key" />
        </el-select>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="shopeTypeValueDialogVisible = false, shopeTypeValue.value = ''">取消</el-button>
          <el-button type="primary" @click="ConfirmMerge">
            确定合并
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
  <div>
    <el-dialog v-model="bulkTakeDeliveryDialogVisible" width="700" height="500">
      <span>
        <el-form :model="bulkTakeDeliveryForm">
          <el-form-item label="拿货数量：">
            <el-input v-model="bulkTakeDeliveryForm.name" style="width: 200px;" @input="everyNumBnt" />
          </el-form-item>
        </el-form>
        <el-table :data="bulkTakeDeliveryTable" border style="width: 100%" max-height="450">
          <el-table-column prop="skuInfo" label="sku信息" width="180" />
          <el-table-column label="商品图" width="180">
            <template #default="scope">
              <el-image style="width: 40px; height: 40px" :src="scope.row.skuPicture" fit="fill" /></template>
          </el-table-column>
          <el-table-column label="拿货数量">
            <template #default="scope"> <input v-model="scope.row.stockGetNum" class="headclass"
                style="width: 90px;border:none" /></template>
          </el-table-column>
          <el-table-column label="档口">
            <template #default="scope">
              <el-select v-model="scope.row.productGearId" clearable>
                <el-option :label="item.value" :value="item.key" v-for="item in scope.row.dangkouVal" :key="item.key" />
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="bulkTakeDeliveryDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="bulkTakeDeliveryDialogVisibleSubmit">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
  <div>
    <el-dialog v-model="bulkSetDialogVisible" width="800">
      <span>
        <el-form :model="bulkSetForm" label-width="auto" inline>
          <el-form-item label="档口名称：">
            <el-input v-model="bulkSetForm.name" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getbulkSetList">搜索</el-button>
            <el-button @click="bulkSetForm.name = '', getbulkSetList()">清除</el-button>
          </el-form-item>
        </el-form>
        <div>
          <el-checkbox-group v-model="checkList">
            <el-checkbox :label="item.id" :value="item.id" v-for="item in productGearListTable" :key="item.key">{{
              item.name }}</el-checkbox>
          </el-checkbox-group>
        </div>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="bulkSetDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="setProductGear">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>

</template>

<script setup>
import { searchProductDic, shopLIst, productPurchaseOrdercreate, productMergeWarehouseSearch, productMergeWarehouseCanUseSearch, productpurchaseUpdate, productpurchaseSearch, productPurchaseReturnedCreatePurchaseReturned, productMergecreateMerge, productGearList, productGearPurchaseCreateBatch, productGearcreateBatch, productGearCanUseList, productGearPurchaseCreate, productMergeWarehouseSave } from '@/api/index'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, onBeforeMount } from 'vue'
import { useRouter } from 'vue-router'
import { useAlertsStore } from '@/store/index'
const store = useAlertsStore()
const router = useRouter()
// form
const form = ref({
  typeId: 0,
  name: '',
  companyGoodIds: [],
  shopGoodIds: [],
  shopGoodSkuIds: [],
  warehouseId: 0,
  company: '',
  // orders: [
  //   // {
  //   //   column: 'cargo_margin',
  //   //   asc: true
  //   // }
  // ],
  pageSize: 10,
  pageNum: 1
})

const onSubmit = () => {
  getList()
}
onBeforeMount(() => {
  getInfo()
  getList()
})

const shopGoodSkuIds = ref('')
const companyGoodIds = ref('')
const shopGoodIds = ref('')
const elRow = ref({})
const getList = () => {
  form.value.shopGoodSkuIds = shopGoodSkuIds.value.split(',').filter(item => item.trim() !== '')
  form.value.companyGoodIds = companyGoodIds.value.split(',').filter(item => item.trim() !== '')
  form.value.shopGoodIds = shopGoodIds.value.split(',').filter(item => item.trim() !== '')
  shopLIst(form.value).then(res => {
    tableData.value = []
    const { data: { data } } = res
    total.value = data.total
    data.list.forEach(element => {
      elRow.value = {}
      const b = element.typeId.toString()
      elRow.value = element
      elRow.value.typeId = b
      tableData.value.push(elRow.value)
    })
    console.log(tableData.value)
    // = data.list
    loading.value = false
  })
}

// 字典
const warehouseDics = ref([])
const productTypeDics = ref([])
const companyDics = ref([])
const getInfo = () => {
  searchProductDic().then(res => {
    const { data: { data } } = res
    warehouseDics.value = data.warehouseDics
    productTypeDics.value = data.productTypeDics
    companyDics.value = data.companyDics
  })
}
// table
const multipleSelection = ref([])
const tableData = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}
// table排序
const or = ref(true)
const SortBtn = () => {
  console.log('排序')
  loading.value = true

  or.value = !or.value
  form.value.orders = [
    {
      column: 'last_sales_num',
      asc: or.value
    }
  ]
  getList()

  // shopLIst(form.value).then(res => {
  //   const { data: { data } } = res
  //   total.value = data.total
  //   tableData.value = data.list
  //   loading.value = false
  // })
}
// 详情
const lookDetails = (row) => {
  console.log('查看详情')
  router.push({ path: '/home/shopDetails', query: { row: JSON.stringify(row), id: row.id }, params: { row: 1 } })
}
// 生成采购单
const productId = ref('')
const GeneratePurchaseOrder = (row) => {
  console.log('采购')
  OrderDialogVisible.value = true
  productId.value = row.id
  companyGoodId.value = row.companyGoodId
  shopGoodId.value = row.shopGoodId
  shopGoodSkuId.value = row.shopGoodSkuId
}
// 采购单弹框
const companyGoodId = ref('')
const shopGoodId = ref('')
const shopGoodSkuId = ref('')
const OrderForm = ref({})
const OrderDialogVisible = ref(false)
const increaseNumber = ref(false)
// const increaseNumberValue = ref(1)
// 确定生成
const OrderDialogOnSubmit = () => {
  if (OrderForm.value.purchaseNum && OrderForm.value.orderNum && OrderForm.value.warehouseId) {
    increaseNumber.value = true
  } else {
    ElMessage.error('需要填充完整')
  }
}
const cancelBtn = () => {
  numsKeyObj.value = {}
  numsKey.value = 1
  increaseNumber.value = false
}
// 保存数量
const everyNum = ref(0)
const purchaseNums = ref([])
const saveOrderNum = () => {
  everyNum.value = 0
  purchaseNums.value = []
  for (const key in numsKeyObj.value) {
    console.log(Number(numsKeyObj.value[key]))
    everyNum.value += Number(numsKeyObj.value[key])
    purchaseNums.value.push(Number(numsKeyObj.value[key]))
  }
  console.log(everyNum.value, OrderForm.value.purchaseNum)
  if (everyNum.value === Number(OrderForm.value.purchaseNum)) {
    productPurchaseOrdercreate({
      productId: productId.value,
      purchaseNum: OrderForm.value.purchaseNum,
      orderNum: OrderForm.value.orderNum,
      warehouseId: OrderForm.value.warehouseId,
      purchaseNums: purchaseNums.value,
      companyGoodId: companyGoodId.value,
      shopGoodId: shopGoodId.value,
      shopGoodSkuId: shopGoodSkuId.value
    }).then(res => {
      OrderDialogVisible.value = false
      ElMessage.success('操作成功')
      getList()
    })
  } else {
    ElMessage.error('检查数量')
  }
}
// 分页
const currentPage4 = ref(1)
const pageSize4 = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  form.value.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  form.value.pageNum = val
  getList()
}
const NUMDom = ref('')
const numsKey = ref(1)
const numsKeyObj = ref({})
const upNum = (NUMDom) => {
  numsKey.value++
}
// 修改数量
const setWarnNum = (row) => {
  console.log(row)
  productpurchaseUpdate({
    typeId: row.typeId,
    stockWarnNum: row.stockWarnNum,
    lastSalesNum: row.lastSalesNum
  }, row.id).then(res => {
    ElMessage.success('操作成功')
    getList()
  })
}

// 档口
const shopeIdsProduct = ref('')
const stallDialogVisible = ref(false)
const openStallDiaog = (row) => {
  shopeIdsProduct.value = row.id
  store.productGearListBtn({
    productId: row.id,
    name: ''
  })
  stallDialogVisible.value = true
}
const purchaseNum = ref('')
const productGearId = ref('')
const productGearPurchaseCreateSubmit = () => {
  productGearPurchaseCreate({
    productId: shopeIdsProduct.value,
    productGearId: productGearId.value,
    purchaseNum: purchaseNum.value
  }).then(res => {
    ElMessage.success('操作成功')
    router.push({ path: '/home/everyView/stallStockAwaitGet' })
  })
}
const stallReturn = () => {
  alert('退货')
}
// 退货信息
const CreateReturntableData = ref([])
const CreateReturndialogVisible = ref(false)
const createReturnOrder = (row) => {
  console.log(row)
  productpurchaseSearch({
    productId: row.id
  }).then(res => {
    const { data: { data } } = res
    CreateReturntableData.value = data
    CreateReturndialogVisible.value = true
  })
}
const CreateReturndialogVisibleSubmit = () => {
  if (multipleSelection.value.length > 0) {
    multipleSelection.value.forEach(e => {
      productPurchaseReturnedCreatePurchaseReturned({
        productPurchaseStorageId: e.productPurchaseStorageId,
        returnedNum: Number(e.processQualityBouncedNum)
      }).then(res => {
        CreateReturndialogVisible.value = false
        ElMessage.success('操作成功')
      })
    })
  } else {
    ElMessage.error('选择数据')
  }
}

// 绑库
const dialogVisibles = ref(false)
// const dialogVisiblesHOuse = ref([{
//   a: '1'
// }])
const setKuqu = (row) => {
  kuqu.value = row
}
const setKuwei = (row) => {
  kuwei.value = row
}
const setWareHouseform = ref()
const WareHouseArr = ref({})
const kuqu = ref([])
const kuwei = ref([])
const shopIdsSons = ref(null)
const BindingRepositoryLocation = (row) => {
  shopIdsSons.value = row.id
  setWareHouseform.value = []
  productMergeWarehouseCanUseSearch(row.id).then(res => {
    const { data: { data } } = res
    console.log('data1')
    console.log(data)
    WareHouseArr.value = data
    productMergeWarehouseSearch(row.id).then(res => {
      const { data: { data } } = res
      console.log('data2')
      console.log(data)
      setWareHouseform.value.push(...data)
      if (data.length === 0) {
        setWareHouseform.value.push({
          id: 0,
          warehouseName: '',
          warehouseAreaName: '',
          name: '',
          isStop: '',
          cargoMargin: 0,
          cloudGoodId: '',
          createTime: ''
        })
      }
      dialogVisibles.value = true
    })
  })
}

// const numsKeys = ref(1)
const upNumw = (NUMDom) => {
  // numsKeys.value++
  setWareHouseform.value.push({
    id: 0,
    warehouseName: '',
    warehouseAreaName: '',
    name: '',
    isStop: '',
    cargoMargin: 0,
    cloudGoodId: '',
    createTime: ''
  })
}
const downNum = (index) => {
  // numsKeys.value--
  setWareHouseform.value.splice(index, 1)
}
const submintDialog = () => {
  const a = []
  setWareHouseform.value.forEach(element => {
    a.push({
      warehouseLocationId: element.id,
      cargoMargin: element.cargoMargin
    })
  })
  productMergeWarehouseSave(shopIdsSons.value, { warehouseSaveUpdateList: a }).then(res => {
    ElMessage.success('操作成功')
    dialogVisibles.value = false
  })
}
const shopeTypeValueDialogVisible = ref(false)
const shopeTypeValue = ref(null)
const shopeTypeValueIds = ref([])
const shopMerge = () => {
  shopeTypeValueIds.value = []
  const firstItem = multipleSelection.value[0]
  if (multipleSelection.value.length >= 2) {
    for (let index = 0; index < multipleSelection.value.length; index++) {
      if (multipleSelection.value[index].warehouseId !== firstItem.warehouseId) {
        ElMessage.error('不允许跨仓合并')
        return
      }
    }
    for (let index = 0; index < multipleSelection.value.length; index++) {
      if (multipleSelection.value[index].typeId !== firstItem.typeId) {
        // ElMessage.error('产品类型不同选择')
        shopeTypeValueDialogVisible.value = true
        return
      }
    }
    // for (let index = 0; index < multipleSelection.value.length; index++) {
    //   if (multipleSelection.value[index].typeId !== multipleSelection.value[index + 1].typeId) {
    //     // ElMessage.error('产品类型不同选择')
    //     shopeTypeValueDialogVisible.value = true
    //     return
    //   } else {
    // console.log('1231231231')
    // console.log(multipleSelection.value[index])

    //   }
    // }
    ConfirmMerge()
    // console.log('1231231231')
  } else {
    ElMessage.error('产品合并必须两个及以上')
  }
}
const ConfirmMerge = () => {
  multipleSelection.value.forEach(element => {
    shopeTypeValueIds.value.push(element.id)
  })
  productMergecreateMerge({
    forceStatus: 0,
    typeId: shopeTypeValue.value ? shopeTypeValue.value : multipleSelection.value[0].typeId,
    productIds: shopeTypeValueIds.value
  }).then(res => {
    console.log('res')
    console.log()
    if (res.data.code === 305) {
      ElMessageBox.confirm(
        '选择的产品sku信息不一致是否强制合并?',
        {
          distinguishCancelAndClose: true,
          confirmButtonText: '是',
          cancelButtonText: '否'
        }
      )
        .then(() => {
          //  打开
          productMergecreateMerge({
            forceStatus: 1,
            typeId: shopeTypeValue.value ? shopeTypeValue.value : multipleSelection.value[0].typeId,
            productIds: shopeTypeValueIds.value
          }).then(res => {
            ElMessage.success('操作成功')
            getList()
          })
        })
        .catch((actiion) => {
          if (actiion === 'cancel') {
            console.log('Changes discarded. Proceeding to a new route.')
          }
        })
    }
  }).catch(e => {
    console.log('e')
    console.log(e)
  })
}

// 批量拿货
const bulkTakeDeliveryDialogVisible = ref(false)
const bulkTakeDeliveryForm = ref({})
const bulkTakeDeliveryTable = ref([])
const bulkTakeDeliveryTableSubmit = ref([])
const bulkTakeDelivery = () => {
  // const val = {
  //   productId: 0,
  //   productGearId: 0,
  //   purchaseNum: 0
  // }

  if (multipleSelection.value.length > 0) {
    bulkTakeDeliveryTable.value = structuredClone(multipleSelection.value)
    bulkTakeDeliveryTable.value.forEach(async e => {
      const { data: { data } } = await productGearList({
        productId: e.id,
        name: ''
      })
      console.log('data')
      console.log(data)
      e.dangkouVal = data
      if (data.length > 0) {
        e.productGearId = data[0].key
      }
    })

    bulkTakeDeliveryDialogVisible.value = true
  } else {
    ElMessage.error('选择数据')
  }
}
// 批量设置档口
const bulkSetDialogVisible = ref(false)
const bulkSetForm = ref({})
const bulkSetFormIds = ref([])
const bulkSet = () => {
  bulkSetFormIds.value = []
  if (multipleSelection.value.length > 0) {
    getbulkSetList()
    multipleSelection.value.forEach(e => {
      bulkSetFormIds.value.push(e.id)
    })
    bulkSetDialogVisible.value = true
  } else {
    ElMessage.error('选择数据')
  }
}
const productGearListTable = ref([])
const checkList = ref([])
const getbulkSetList = () => {
  // const checkList = []
  productGearCanUseList({
    productId: 0,
    name: bulkSetForm.value.name
  }).then(res => {
    const { data: { data } } = res
    data.forEach(element => {
      if (element.isBind) {
        // checkList.push(element.id)
      }
    })
    productGearListTable.value = data
  })
}
const setProductGear = () => {
  productGearcreateBatch({
    productIds: bulkSetFormIds.value,
    productGearIds: checkList.value
  }).then(res => {
    bulkSetDialogVisible.value = false
    ElMessage.success('操作成功')
  })
}

const everyNumBnt = () => {
  bulkTakeDeliveryTable.value.forEach(e => {
    e.stockGetNum = bulkTakeDeliveryForm.value.name
  })
}
const bulkTakeDeliveryDialogVisibleSubmit = () => {
  bulkTakeDeliveryTable.value.forEach(e => {
    bulkTakeDeliveryTableSubmit.value.push({
      productId: e.id,
      productGearId: e.productGearId,
      purchaseNum: e.stockGetNum
    })
  })
  console.log(bulkTakeDeliveryTableSubmit.value)
  productGearPurchaseCreateBatch(bulkTakeDeliveryTableSubmit.value).then(res => {
    getList()
    ElMessage.success('操作成功')
  })
}
const loading = ref(false)

const tableRowClassName = ({
  row,
  rowIndex
}) => {
  console.log(row)
  if (row.stockNum <= row.stockWarnNum && row.stockWarnNum !== 0) {
    console.log(row)
    return 'warning-row'
  }
  return ''
}

</script>

<style lang='less' scoped>
.NUM {
  width: 800px;
  min-height: 200px;
  border: 1px solid #ccc;
  margin: 0 auto;
  padding: 40px;
}

.tableDiv {
  margin-top: 10px;
}

.colors {
  color: #DF868F
}
</style>
